package com.witdata.common.service.subject.impl;

import com.witdata.common.domain.dto.SqlCheckDto;
import com.witdata.mysql.base.service.SqlCheckBaseService;
import com.witdata.mysql.base.service.SubjectBaseService;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.SubjectDto;
import com.witdata.mysql.domain.entity.TSqlCheck;
import com.witdata.mysql.domain.entity.TSubject;
import com.witdata.common.domain.vo.UserVo;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.mapper.SubjectMapper;
import com.witdata.common.service.FileService;
import com.witdata.common.service.UserService;
import com.witdata.common.service.subject.SubjectService;
import com.witdata.common.util.IdUtils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.mybatisflex.core.query.QueryWrapper;
import com.witdata.mysql.base.service.SubjectQuestionBaseService;

@Service
@Slf4j
@RequiredArgsConstructor
public class SubjectServiceImpl implements SubjectService {
    private final SubjectBaseService subjectBaseService;
    private final SubjectQuestionBaseService subjectQuestionBaseService;
    private final FileService fileService;
    private final UserService userService;
    private final SqlCheckBaseService sqlCheckBaseService;
    private final IdUtils idUtils;

    @Override
    public List<SubjectDto> getSubjectList() {
        return getSubjectList(new QueryWrapper());
    }

    @Override
    public List<SubjectDto> getSubjectList(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper().eq("creator", userId);
        return getSubjectList(queryWrapper);
    }

    public List<SubjectDto> getSubjectList(QueryWrapper queryWrapper) {
        List<TSubject> tSubjectList = subjectBaseService.list(queryWrapper);
        if (CollectionUtils.isEmpty(tSubjectList)) return List.of();
        List<SubjectDto> subjectDtoList = new ArrayList<>();
        tSubjectList.forEach(tSubject -> {
            SubjectDto subjectDto = getSubjectInfoById(tSubject.getId());
            subjectDtoList.add(subjectDto);
        });
        return subjectDtoList;
    }

    @Override
    @Transactional
    public void createSubject(String name, MultipartFile avatar) {
        Long id = IdUtils.createId();
        String url = fileService.uploadFile(avatar, "/subject/set/" + id);
        TSubject tSubject = TSubject.builder()
                .id(id)
                .name(name)
                .avatar(url)
                .creator(idUtils.getUserIdByRequest())
                .created(LocalDateTime.now())
                .updated(LocalDateTime.now())
                .build();
        boolean save = subjectBaseService.save(tSubject);
        if (!save) throw new GlobalException(Status.SAVEERROR);
    }

    @Override
    public SubjectDto getSubjectInfo() {
        return null;
    }

    @Override
    public SubjectDto getSubjectInfoById(Long id) {
        TSubject tSubject = subjectBaseService.getById(id);
        UserVo userVo = userService.getUserVoById(tSubject.getCreator());
        SubjectDto SubjectDto = SubjectMapper.INSTANCE.toSubjectDto(tSubject);
        SubjectDto.setCreator(userVo);
        return SubjectDto;
    }

    @Override
    public void removeSubject(Long id) {
        // 根据id删除所有题目
        subjectQuestionBaseService.remove(new QueryWrapper().eq("subject_id", id));
        // 根据id删除题集
        subjectBaseService.removeById(id);
    }

    @Override
    public List<SqlCheckDto> getSQLCheckList(Long subjectId) {
        QueryWrapper queryWrapper = new QueryWrapper().eq("subject_id", subjectId);
        List<TSqlCheck> list = sqlCheckBaseService.list(queryWrapper);
        return list.stream().map(item -> SqlCheckDto.builder()
                .userId(item.getUserId())
                .subjectId(item.getSubjectId())
                .questionId(item.getQuestionId())
                .content(item.getContent())
                .createTime(item.getCreateTime())
                .multiplicity(item.getMultiplicity())
                .build()).toList();
    }
}
